package com.riku.security.service.impl;

import com.riku.security.common.SeSecurityException;
import com.riku.security.common.SnowFlake;
import com.riku.security.mapper.SeRoleMapper;
import com.riku.security.mapper.SeRoleMenuMapper;
import com.riku.security.mapper.SeRoleUserMapper;
import com.riku.security.model.SeRole;
import com.riku.security.model.SeRoleMenu;
import com.riku.security.model.SeRoleUser;
import com.riku.security.service.SeRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by NANC on 2021/5/16.
 */
@Service
public class SeRoleServiceImpl implements SeRoleService {
    @Autowired
    private SeRoleMapper seRoleMapper;

    @Autowired
    private SeRoleMenuMapper seRoleMenuMapper;

    @Autowired
    private SeRoleUserMapper seRoleUserMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    @Transactional
    public void insertSeRole(String serviceCode, String roleName,String descr,Integer status,String menuIds) {
        SeRole seRole = new SeRole();
        seRole.setId(snowFlake.nextId());
        seRole.setServiceCode(serviceCode);
        seRole.setRoleName(roleName);
        seRole.setStatus(status);
        seRole.setDescr(descr);

        seRoleMapper.insert(seRole);

        if (menuIds != null && !"".equals(menuIds)) {
            for (String menuId : menuIds.split(",")) {
                SeRoleMenu rm = new SeRoleMenu();
                rm.setId(snowFlake.nextId());
                rm.setMenuId(Long.parseLong(menuId));
                rm.setRoleId(seRole.getId());
                rm.setServiceCode(serviceCode);
                seRoleMenuMapper.insert(rm);
            }
        }
    }

    @Override
    @Transactional
    public void updateRole(String serviceCode,Long id, String roleName,String descr,Integer status,String menuIds) {
        SeRole seRole = new SeRole();
        seRole.setId(id);
        seRole.setRoleName(roleName);
        seRole.setStatus(status);
        seRole.setDescr(descr);

        seRoleMapper.updateByPrimaryKeySelective(seRole);

        seRoleMenuMapper.deleteByRoleId(id);
        if (menuIds != null && !"".equals(menuIds)) {
            for (String menuId : menuIds.split(",")) {
                SeRoleMenu rm = new SeRoleMenu();
                rm.setId(snowFlake.nextId());
                rm.setMenuId(Long.parseLong(menuId));
                rm.setRoleId(id);
                rm.setServiceCode(serviceCode);
                seRoleMenuMapper.insert(rm);
            }
        }
    }

    @Override
    public void statusRole(Long id, Integer status) {
        SeRole seRole = new SeRole();
        seRole.setId(id);
        seRole.setStatus(status);

        seRoleMapper.updateByPrimaryKeySelective(seRole);
    }

    @Override
    public void insertRoleUser(String serviceCode,Long roleId,String userId,String userName) {
        SeRoleUser query = new SeRoleUser();
        query.setRoleId(roleId);
        query.setUserId(userId);

        int count = seRoleUserMapper.countByRoleIdUserId(query);
        if (count > 0) {
            throw new SeSecurityException(SeSecurityException.ERROR_EXSIT);
        }

        SeRoleUser ru = new SeRoleUser();
        ru.setId(snowFlake.nextId());
        ru.setRoleId(roleId);
        ru.setServiceCode(serviceCode);
        ru.setUserId(userId);
        ru.setUserName(userName);
        seRoleUserMapper.insert(ru);
    }

    @Override
    public void deleteRoleUser(Long id) {
        seRoleUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteSeRoleById(Long id) {
        seRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SeRole> selectRoleList(String serviceCode,String roleName) {
        SeRole query = new SeRole();
        query.setServiceCode(serviceCode);
        query.setRoleName(roleName);

        List<SeRole> list = seRoleMapper.selectByName(query);
        return list;
    }

    @Override
    public List<SeRoleUser> selectRoleUserList(Long roleId,String userName) {
        SeRoleUser ru = new SeRoleUser();
        ru.setRoleId(roleId);
        ru.setUserName(userName);
        return seRoleUserMapper.selectRoleUserList(ru);
    }

    @Override
    public List<SeRoleMenu> selectRoleMenuList(Long id) {
        return seRoleMenuMapper.selectByRoleId(id);
    }
}
